package com.itboye.eweilisten.me.personal.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.adapter.ParentAdapter;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.model.ChildEntity;
import com.itboye.eweilisten.model.GoodsDetailBean;
import com.itboye.eweilisten.model.ParentEntity;
import com.itboye.eweilisten.util.widget.XImageLoader;

import java.util.ArrayList;

/**
 * Created by itboye on 2017/3/11.
 * 立即体验
 */

public class ActivityCatalog extends BaseActivity implements ExpandableListView.OnGroupExpandListener,
        ParentAdapter.OnChildTreeViewClickListener {
    private ExpandableListView listView;
    private Context mContext;
    private ArrayList<ParentEntity> parents;
    private ParentAdapter adapter;
    private String Uint[] = {"Uint 1 Hello!", "Uint 2 Hello!", "Uint 3 Hello!"};
    private  String TextA[] = {"TextABean 单词、词组", "TextB 单词、词组", "TextC 单词、词组", "TextD 单词、词组"};
    private  String Name[] = {"(一)听录音组合单词", "(二)听录音选择正确的图片", "(三)听录音选择听到的字母、单词或词组", "(四)听录音按听到的顺序排列单词", "(五)听录音看图片与听到是否一致", "(六)听录音听写单词没空一个字母"};
    private ImageView imgBook;
    private ImageView back;
    private TextView tvTitle;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_experience);
        listView = (ExpandableListView) findViewById(R.id.list);
        mContext = this;
        loadData();
//        initEList();
        XImageLoader.load("http://imgstatic.baidu.com/img/image/shouye/fanbingbing.jpg",imgBook);
    }
    /**
     *
     *         初始化菜单数据源
     * */
    private void loadData() {
        tvTitle.setText("微测试目录");
        parents = new ArrayList<ParentEntity>();
        for (int i = 0; i < Uint.length; i++) {
            ParentEntity parent = new ParentEntity();
            parent.setGroupName(Uint[i]);
            ArrayList<ChildEntity> childs = new ArrayList<ChildEntity>();
            for (int j = 0; j < TextA.length; j++) {
                ChildEntity child = new ChildEntity();
                child.setGroupName(TextA[j]);
                ArrayList<String> childNames = new ArrayList<String>();
                for (int k = 0; k <Name.length; k++) {
                    childNames.add(Name[k]);
                }
                child.setChildNames(childNames);
                childs.add(child);
            }
            parent.setChilds(childs);
            parents.add(parent);

        }
    }

    /**
     *
     *         初始化ExpandableListView
     * */
    private void initEList(GoodsDetailBean bean) {
        listView.setOnGroupExpandListener(this);

        adapter = new ParentAdapter(mContext,bean,false);

        listView.setAdapter(adapter);

        adapter.setOnChildTreeViewClickListener(this);

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.back:
                this.finish();
                break;
        }
    }

    /**
     *
     *         点击子ExpandableListView的子项时，回调本方法，根据下标获取值来做相应的操作
     * */
    @Override
    public void onClickPosition(int parentPosition, int groupPosition,
                                int childPosition) {
        // do something
        String childName = parents.get(parentPosition).getChilds()
                .get(groupPosition).getChildNames().get(childPosition)
                .toString();
        Toast.makeText(
                mContext,
                "点击的下标为： parentPosition=" + parentPosition
                        + "   groupPosition=" + groupPosition
                        + "   childPosition=" + childPosition + "\n点击的是："
                        + childName, Toast.LENGTH_SHORT).show();
    }

    /**
     *
     *         展开一项，关闭其他项，保证每次只能展开一项
     * */
    @Override
    public void onGroupExpand(int groupPosition) {
        for (int i = 0; i < parents.size(); i++) {
            if (i != groupPosition) {
                listView.collapseGroup(i);
            }
        }
    }
}
